#include "ClassFeatureProxy.h"



#include <CharacterItem.h>
#include <ItemReference.h>



#include <QDebug>



ClassFeatureProxy::ClassFeatureProxy(QObject *parent) :
    AbstractItemProxy(parent)
{
}


int ClassFeatureProxy::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
//    return 1;
    return (Field_LAST - Field_FIRST + 1);
}

QVariant ClassFeatureProxy::data(const QModelIndex &proxyIndex, int role) const
{
    if (!proxyIndex.isValid() || role != Qt::DisplayRole) return QVariant();

    QModelIndex sourceIndex = mapToSource(proxyIndex);
    if (!sourceIndex.isValid()) return QVariant();

    switch (proxyIndex.column())
    {
    case NameField:     return sourceIndex.data(Qt::DisplayRole);
    case ValueField:    break;
    default:            return QVariant();
    }

    return QVariant();
}


bool ClassFeatureProxy::setData(const QModelIndex &index, const QVariant &value, int role)
{
    return false;
}


QVariant ClassFeatureProxy::headerData(int section, Qt::Orientation orientation, int role) const
{
    Q_UNUSED(orientation);
    if (role != Qt::DisplayRole) return QVariant();

    switch (section)
    {
    case NameField:     return tr("Feature");
    case ValueField:    return tr("Value");
    }

    return QVariant();
}


Qt::ItemFlags ClassFeatureProxy::flags(const QModelIndex &index) const
{
    if (index.column() != ValueField) return AbstractItemProxy::flags(index);

    return AbstractItemProxy::flags(index) | Qt::ItemIsEditable;
}



void ClassFeatureProxy::remap()
{
    qDebug() << Q_FUNC_INFO;

    beginResetModel();
    m_indexMap.clear();
    endResetModel();

    ItemModel* model = itemModel();
    if (!model) return;

    CharacterItem* root = dynamic_cast<CharacterItem*>(model->rootItem());
    if (!root || root->campaignName().isEmpty()) return;

    QString pattern = QString("%1.%2.Class Feature.([a-zA-Z0-9 ]+)").arg(root->name()).arg(root->campaignName());
    qDebug() << "Searching Class Features with" << pattern;
    QModelIndex matchStart = model->index(0, AbstractItem::IdData, QModelIndex());
    QModelIndexList foundIndexes = model->match(matchStart,
                                                Qt::EditRole,
                                                pattern,
                                                -1,
                                                Qt::MatchRegExp |
                                                Qt::MatchRecursive);


    emit layoutAboutToBeChanged();
    foreach (QModelIndex foundIndex, foundIndexes)
    {
        //qDebug() << "Found Class Feature:" << foundIndex.data(Qt::EditRole).toString();
        QModelIndex nameSourceIndex = foundIndex.sibling(foundIndex.row(), AbstractItem::NameData);
        QModelIndex nameProxyIndex = createIndex(foundIndex.row(), NameField, nameSourceIndex.internalPointer());

        m_indexMap.insert(nameSourceIndex, nameProxyIndex);
        m_indexMap.insert(foundIndex, createIndex(foundIndex.row(), ValueField, foundIndex.internalPointer()));

        QModelIndex refIndex = foundIndex.child(0, AbstractItem::IdData);
        while (refIndex.isValid())
        {
            IndexMap childMap = mapChildren(refIndex);
            if (!childMap.isEmpty()) m_childMap.insert(nameProxyIndex, childMap);

            refIndex = refIndex.sibling(refIndex.row()+1, refIndex.column());
        }

    }
    emit layoutChanged();
}



IndexMap ClassFeatureProxy::mapChildren(const QModelIndex& sourceReferenceIndex)
{
    qDebug() << Q_FUNC_INFO;

    QModelIndex valueIndex = sourceReferenceIndex.sibling(sourceReferenceIndex.row(), ItemReference::ReferenceData);
    if (!valueIndex.isValid()) return IndexMap();

    ItemModel* model = itemModel();
    if (!model) return IndexMap();

    CharacterItem* root = dynamic_cast<CharacterItem*>(model->rootItem());
    if (!root || root->campaignName().isEmpty()) return IndexMap();

    QString pattern = QString("%1.%2")
                      .arg(root->name())
                      .arg(valueIndex.data(Qt::DisplayRole).toString());

    qDebug() << "Searching Feature References with:" << pattern;
    QModelIndex matchStart = model->index(0, AbstractItem::IdData, QModelIndex());
    QModelIndexList foundIndexes = model->match(matchStart,
                                                Qt::EditRole,
                                                pattern,
                                                -1,
                                                Qt::MatchExactly |
                                                Qt::MatchRecursive);

    foreach (QModelIndex foundIndex, foundIndexes)
    {
        qDebug() << "Found Reference:" << foundIndex.data(Qt::EditRole).toString();
    }

    return IndexMap();
}
